<?php
session_start();
//生成验证码图片
Header("Content-type: image/PNG");
$im = imagecreate(44,18);
$back = ImageColorAllocate($im, 245,245,245);
imagefill($im,0,0,$back); //背景
mt_srand((double)microtime()*1000000);
//生成4位数字
$codes = '';
$authNum = 0;
$font='';
for($i=0;$i<4;$i++){
    try {
        $font = ImageColorAllocate($im, random_int(100, 255), random_int(0, 100), random_int(100, 255));
    } catch (Exception $e) {
    }
    try {
        $authNum = random_int(1, 9);
    } catch (Exception $e) {
    }
    $codes .= $authNum;
    imagestring($im, 5, 2+$i*10, 1, $authNum, $font);
}
$randColor = '';
for($i=0;$i<100;$i++) //加入干扰象素
{
    try {
        $randColor = ImageColorallocate($im, random_int(0, 255), random_int(0, 255), random_int(0, 255));
    } catch (Exception $e) {
    }
    try {
        imagesetpixel($im, random_int(0, 44), random_int(0, 18), $randColor);
    } catch (Exception $e) {

    }
}
ImagePNG($im);
ImageDestroy($im);
$_SESSION['code'] = $codes;
